home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / animate / animate.C < prev    next >
C/C++ Source or Header  |  1992-12-01  |  15KB  |  725 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. extern "C" {
  6. int sginap(long);
  7. #include <forms.h>
  8. #include "anim.h"
  9. #include "glob.h"
  10. }
  11. #include "animate.h"
  12.  
  13.  
  14. void showusage()
  15. {
  16.  fprintf(stderr,"Incorrect command line arguments.\n");
  17.  fprintf(stderr,"Usage: animate [-w posx posy] [-p] [-b] [-s speed] [-o] [-S script] [-hide]\n\t\t[-f file1 file2 ...]\n");
  18.  exit(0);
  19. }
  20.  
  21. script::script()
  22. {
  23.  maxline = 0;
  24.  linenum = 0;
  25.  looping = 0;
  26.  bouncing = 0;
  27.  playing = 0;
  28.  speed = 0;
  29.  slow = 0;
  30.  direct = 1;
  31.  topline = 1;
  32.  my_browser = NULL;
  33.  loaded=0;
  34.  gonce=0;
  35.  commnd[0]='\0';
  36. }
  37.  
  38. void script::once()
  39. {
  40.  gonce = !gonce;
  41. }
  42.  
  43. script::~script()
  44. {
  45.  // do nothing;
  46. }
  47.  
  48. void script::init(int numlines, FL_OBJECT *brow)
  49. {
  50.  if (!numlines) return;
  51.  my_browser = brow;
  52.  maxline = numlines;
  53.  playing = 0;
  54.  if (!loaded)
  55.  {
  56.   fprintf(stdout,"(geometry animate : anim)\n");
  57.   fflush(stdout);
  58.  }
  59.  loaded = 1;
  60.  linenum = 0;
  61. }
  62.  
  63. void script::start_playing()
  64. {
  65.  playing = 1;
  66. }
  67.  
  68. void script::stop_playing()
  69. {
  70.  playing = 0;
  71. }
  72.  
  73. void script::toggle_looping()
  74. {
  75.  looping = !looping;
  76. }
  77.  
  78. void script::change_speed(int spd)
  79. {
  80.   speed = spd;
  81. }
  82.  
  83. void script::set_linenum(int lne)
  84. {
  85.   char *ln;
  86.  
  87.   fl_freeze_object(my_browser);
  88.   if (linenum)
  89.   {
  90.    ln = fl_get_browser_line(my_browser, linenum);
  91.    if (ln[0]=='@')
  92.     fl_replace_browser_line(my_browser, linenum, &(ln[3]));
  93.   }
  94.   linenum = lne;
  95.   ln = fl_get_browser_line(my_browser, linenum);
  96.   sprintf(line,"@C1%s",ln);
  97.   fl_replace_browser_line(my_browser, linenum, line);
  98.   fl_unfreeze_object(my_browser);
  99.   show_geom(linenum);
  100. }
  101.  
  102. void script::change_direction(int dir)
  103. {
  104.  if ((dir==1)||(dir==(-1)))
  105.   direct = dir;
  106.  else
  107.  {
  108.   if (direct==1)
  109.    direct = -1;
  110.   else
  111.    direct = 1;
  112.  }
  113. }
  114.  
  115. void script::toggle_bounce()
  116. {
  117.  bouncing = !bouncing;
  118. }
  119.  
  120. void script::do_step()
  121. {
  122.  char *ln;
  123.  int   old;
  124.  int   nline;
  125.  
  126.  if (!maxline) return;
  127.  fl_freeze_object(my_browser);
  128.  if ((!looping)&&(!bouncing))
  129.  {
  130.  if (gonce&&(((linenum==1)&&(direct==(-1)))||((linenum==maxline)&&(direct==1))))
  131.   {
  132.    stop_playing();
  133.   }
  134.   else {
  135.   if (linenum)
  136.   {
  137.    ln = fl_get_browser_line(my_browser, linenum);
  138.    if (ln[0]=='@')
  139.     fl_replace_browser_line(my_browser, linenum, &(ln[3]));
  140.   }
  141.   linenum+=direct;
  142.   if (linenum<1) linenum = maxline;
  143.   if (linenum>maxline) linenum = 1;
  144.   ln = fl_get_browser_line(my_browser, linenum);
  145.   sprintf(line,"@C1%s",ln);
  146.   fl_replace_browser_line(my_browser, linenum, line);
  147.   show_geom(linenum);
  148.   }
  149.  }
  150.  else
  151.  if ((!looping)&&(bouncing))
  152.  {
  153.  if (gonce&&(((linenum==1)&&(direct==(-1)))||((linenum==maxline)&&(direct==1))))
  154.   {
  155.    stop_playing();
  156.   }
  157.   else {
  158.   if (linenum)
  159.   {
  160.    ln = fl_get_browser_line(my_browser, linenum);
  161.    if (ln[0]=='@')
  162.     fl_replace_browser_line(my_browser, linenum, &(ln[3]));
  163.   }
  164.   linenum+=direct;
  165.   if (linenum<1)
  166.   {
  167.    linenum = 2;
  168.    direct = 1;
  169.   }
  170.   if (linenum>maxline)
  171.   {
  172.    linenum = maxline-1;
  173.    direct = -1;
  174.   }
  175.   if (maxline==1) linenum = 1;
  176.   ln = fl_get_browser_line(my_browser, linenum);
  177.   sprintf(line,"@C1%s",ln);
  178.   fl_replace_browser_line(my_browser, linenum, line);
  179.   show_geom(linenum);
  180.  }
  181.  }
  182.  else
  183.  if ((looping)&&(!bouncing))
  184.  {
  185.   old = linenum;
  186.   linenum+=direct;
  187.   if (gonce&&((linenum<1)||(linenum>maxline)))
  188.   {
  189.    stop_playing();
  190.    linenum = old;
  191.   }
  192.   else
  193.   {
  194.    if (linenum<1) linenum = maxline;
  195.    if (linenum>maxline) linenum = 1;
  196.    while ((!fl_isselected_browser_line(my_browser,linenum))&&(linenum!=old)&&playing)
  197.    {
  198.     linenum+=direct;
  199.     if (gonce&&((linenum<1)||(linenum>maxline)))
  200.     {
  201.      stop_playing();
  202.      linenum=old;
  203.     }
  204.     if (playing)
  205.     {
  206.      if (linenum<1) linenum = maxline;
  207.      if (linenum>maxline) linenum = 1;
  208.     }
  209.    }
  210.    nline=linenum;
  211.    linenum=old;
  212.    set_linenum(nline);
  213.   }
  214.  }
  215.  else
  216.  if (looping&&bouncing)
  217.  {
  218.   old = linenum;
  219.   linenum+=direct;
  220.   if (gonce&&((linenum<1)||(linenum>maxline)))
  221.   {
  222.    stop_playing();
  223.    linenum = old;
  224.   }
  225.   if (linenum<1) {linenum = 1;change_direction(0);}
  226.   if (linenum>maxline) {linenum = maxline;change_direction(0);}
  227.   while ((!fl_isselected_browser_line(my_browser,linenum))&&(linenum!=old))
  228.   {
  229.    linenum+=direct;
  230.    if (gonce&&((linenum<1)||(linenum>maxline)))
  231.    {
  232.     stop_playing();
  233.     linenum=old;
  234.    }
  235.    if (playing)
  236.    {
  237.     if (linenum<1) {linenum = 1;change_direction(0);}
  238.     if (linenum>maxline) {linenum = maxline;change_direction(0);}
  239.    }
  240.   }
  241.   nline=linenum;
  242.   linenum=old;
  243.   set_linenum(nline);
  244.  }
  245.  if ((linenum<topline)||(linenum>(topline+6)))
  246.  {
  247.   topline+=direct;
  248.   if ((linenum<topline)||(linenum>(topline+6)))
  249.    topline = linenum;
  250.   fl_set_browser_topline(my_browser,topline);
  251.  }
  252.  fl_unfreeze_object(my_browser);
  253. }
  254.  
  255. void script::add_command(char *incoming)
  256. {
  257.  strcpy(commnd,incoming);
  258. }
  259.  
  260. void script::animate()
  261. {
  262.  if (speed==1000)
  263.   return;
  264.  if (playing)
  265.  {
  266.   sginap((long)speed/50);
  267.   slow+=50;
  268.   if (slow>speed)
  269.   {
  270.    slow=0;
  271.    do_step();
  272.   }
  273.  }
  274. }
  275.  
  276. void script::clean()
  277. {
  278.  int i;
  279.  
  280.  if (loaded)
  281.  for (i=1;i<(maxline+1);i++)
  282.  {
  283.   fprintf(stdout,"(read geometry { define a%d { LIST } } )\n",i);
  284.   fflush(stdout);
  285.  }
  286. }
  287.  
  288. void script::show_geom(int linenum)
  289. {
  290.  char c = ' ';
  291.  
  292.  fprintf(stdout,"(echo x)\n");
  293.  fprintf(stdout,"(read geometry { define anim { LIST : a%d } } )\n",linenum);
  294.  if (commnd[0]!='\0')
  295.    fprintf(stdout,commnd,linenum);
  296.  fflush(stdout);
  297.  while ((c!='x')&&(!feof(stdin)))
  298.  {
  299.   c = (char) fgetc(stdin);
  300.  }
  301. }
  302.  
  303. void err_msg(char *msg)
  304. {
  305.  fprintf(stderr,"%s\n",msg);
  306. }
  307.  
  308. void get_file(FL_OBJECT *browse, char *filename, int *maxlines)
  309. {
  310.  FILE *thisfile;
  311.  char line[256],ch=' ';
  312.  char tmpline[257];
  313.  int  chnum=0,spc=1,pos=0;
  314.  
  315.  (*maxlines) = 0;
  316.  fl_clear_browser(browse);
  317.  if (!(thisfile=fopen(filename,"r")))
  318.  {
  319.   err_msg("Couldn\'t open file.");
  320.   return;
  321.  }
  322.  fl_freeze_object(browse);
  323.  while (!feof(thisfile))
  324.  {
  325.   spc=1;chnum=0;pos=0;ch=' ';
  326.   while ((chnum<256)&&(ch!='\n')&&(!feof(thisfile)))
  327.   {
  328.    line[chnum] = ch = (char) fgetc(thisfile);
  329.    chnum++;
  330.    if (ch=='#')
  331.    {
  332.     while ((!feof(thisfile))&&(ch!='\n'))
  333.     {
  334.      ch = (char) fgetc(thisfile);
  335.     }
  336.     ch = '\n';
  337.    }
  338.    if ((ch!=' ')&&(ch!='\t')&&(ch!='\n')&&spc)
  339.    {
  340.     spc=0;
  341.     pos=chnum-1;
  342.    }
  343.   }
  344.   if (!spc)
  345.   {
  346.    line[chnum-1] = '\0';
  347.    chnum=pos;
  348.    while((line[pos]!=' ')&&(line[pos]!='\t')&&(line[pos]!='\0'))
  349.     pos++;
  350.    line[pos]='\0';
  351.    if (pos!=chnum)
  352.     {
  353.      fprintf(stdout,"(read geometry { define a%d < \"%s\" } )\n",
  354.      (*maxlines)+1,&(line[chnum]));
  355.      fflush(stdout);
  356.      sprintf(tmpline," %s",&(line[chnum]));
  357.      fl_add_browser_line(browse,tmpline);
  358.      (*maxlines)++;
  359.     }
  360.   }
  361.  }
  362.  fl_unfreeze_object(browse);
  363.  fclose(thisfile);
  364. }
  365.  
  366. void read_glob(FL_OBJECT *browse, char **gfiles, int *maxlines)
  367. {
  368.  int  fnum=0;
  369.  char tmpline[256];
  370.  char ret;
  371.  FL_OBJECT *retobj;
  372.  
  373.  fl_clear_browser(browse);
  374.  while ((gfiles[fnum]!=NULL)&&((*gfiles[fnum])!='\0'))
  375.  {
  376.   fprintf(stdout,"(read geometry { define a%d < \"%s\" } )\n",
  377.       fnum+1,gfiles[fnum]);
  378.   fprintf(stdout,"(read geometry { define anim { LIST : a%d } } )\n",fnum+1);
  379.   fprintf(stdout,"(echo q)");
  380.   fflush(stdout);
  381.   fscanf(stdin,"%c",&ret);
  382.   if (ret!='q')
  383.    return;
  384.   retobj = fl_check_forms();
  385.   if (retobj==ExitButton)
  386.    exit(0);
  387.   sprintf(tmpline," %s",gfiles[fnum]);
  388.   fl_add_browser_line(browse,tmpline);
  389.   fnum++;
  390.  }
  391.  (*maxlines) = fnum;
  392. }
  393.  
  394.  
  395. void set_info(FL_OBJECT *myinfo)
  396. {
  397.  int i;
  398.  static char *inf[] = {
  399.  "By Daeron Meyer",
  400.  "Copyright (c) 1992",
  401.  "The Geometry Center",
  402.  "anonymous ftp: geom.umn.edu",
  403.  "email: software@geom.umn.edu",
  404.  " ",
  405.  "Geom-Animator is free software and is",
  406.  "designed to be used as an animation tool",
  407.  "for Geomview. It provides several simple",
  408.  "animation controls such as looping, bouncing,",
  409.  "and single frame stepping. A brief description",
  410.  "of functionality is included but for a more",
  411.  "complete tutorial, read the man pages.",
  412.  " ",
  413.  "To read in a list of frames, enter their names",
  414.  "in the input box at the bottom of the main panel",
  415.  "and press return or click on Load. To read in a",
  416.  "script containing a list of frames type the name",
  417.  "of the script in the input box and click on the",
  418.  "Script button.",
  419.  " ",
  420.  "Once the frames have been read in (the",
  421.  "animate object will appear in the geomview",
  422.  "object browser) one can scroll through the",
  423.  "frames by clicking on play or clicking on the",
  424.  "forward or backward step buttons. Clicking on",
  425.  "stop will cause animator to stop playing frames.",
  426.  " ",
  427.  "The speed at which frames are playing can be",
  428.  "adjusted using the speed slider. Playing slowly",
  429.  "through the frames can also be accomplished",
  430.  "by holding one of the step buttons down.",
  431.  " ",
  432.  "Clicking on the once button will make animator",
  433.  "go through a sequence of frames once, and",
  434.  "then cause it to stop on the last or first frame.",
  435.  " ",
  436.  "Toggling the bounce button on will put animator",
  437.  "in bounce mode. This means that once the last",
  438.  "or first frame is reached (while playing frames)",
  439.  "the direction of play will be automatically",
  440.  "reversed.",
  441.  " ",
  442.  "Toggling the loop button on will put animator",
  443.  "in loop mode. In this mode, animator confines",
  444.  "itself to looping through only the frames that",
  445.  "have been outlined in the frame browser."
  446.  };
  447.  for (i=0;i<47;i++)
  448.   fl_add_browser_line(myinfo, inf[i]);
  449. }
  450.  
  451.  
  452.  
  453. //------------------------------------------------------
  454.  
  455. main(int argc, char *argv[])
  456. {
  457.  int        maxlines=0;
  458.  int        line=0;
  459.  int        hidden=0;
  460.  FL_OBJECT *retobj=NULL;
  461.  script     myscript;
  462.  char     **fils;
  463.  int        fnum,tmp,filesin = 0,scriptin = 0,playin=0;
  464.  int        block=1;
  465.  int        argnum=1;
  466.  int        wposx = -1, wposy = -1;
  467.  
  468.  foreground();
  469.  fl_init();
  470.  create_the_forms(); // create the forms
  471.  fl_set_slider_bounds(SpeedSlide,0,1000);
  472.  fl_set_slider_value(SpeedSlide,1000);
  473.  
  474.  set_info(InfoBrowse);
  475.  
  476.  if (argc>1)
  477.   while (argnum<argc)
  478.   {
  479.     if (*argv[argnum] == '-')
  480.     {
  481.       if (!strcmp(argv[argnum],"-w"))
  482.       {
  483.     if ((argc-argnum)<3) showusage();
  484.         if (!sscanf(argv[argnum+1],"%d",&wposx) ||
  485.         !sscanf(argv[argnum+2],"%d",&wposy)) showusage();
  486.     argnum+=3;
  487.       }
  488.       else
  489.       if (!strcmp(argv[argnum],"-p"))
  490.       {
  491.     playin = 1;
  492.         block=0;
  493.     argnum++;
  494.       }
  495.       else
  496.       if (!strcmp(argv[argnum],"-b"))
  497.       {
  498.     myscript.toggle_bounce();
  499.     fl_set_button(BounceButton, 1);
  500.     argnum++;
  501.       }
  502.       else
  503.       if (!strcmp(argv[argnum],"-s"))
  504.       {
  505.     if ((argc-argnum)<2) showusage();
  506.     if (!sscanf(argv[argnum+1],"%d",&tmp)) showusage();
  507.  
  508.     tmp *= 10;
  509.     if (tmp<0) tmp = 0;
  510.     if (tmp>1000) tmp = 1000;
  511.     fl_set_slider_value(SpeedSlide,tmp);
  512.     myscript.change_speed(1000-tmp);
  513.     argnum+=2;
  514.       }
  515.       else
  516.       if (!strcmp(argv[argnum],"-hide"))
  517.       {
  518.     hidden = 1;
  519.     argnum++;
  520.       }
  521.       else
  522.       if (!strcmp(argv[argnum],"-o"))
  523.       {
  524.     myscript.once();
  525.     fl_set_button(OnceButton, 1);
  526.     argnum++;
  527.       }
  528.       else
  529.       if (!strcmp(argv[argnum],"-f"))
  530.       {
  531.     if ((argc-argnum)<2) showusage();
  532.     fils = &(argv[argnum+1]);
  533.     argnum = argc;
  534.     filesin = 1;
  535.       }
  536.       else
  537.       if (!strcmp(argv[argnum],"-S"))
  538.       {
  539.     if ((argc-argnum)<2) showusage();
  540.         fprintf(stdout,"(read geometry { define anim { LIST } } )\n");
  541.         fprintf(stdout,"(geometry animate : anim)\n");
  542.         fflush(stdout);
  543.         myscript.clean();
  544.         get_file(ScriptBrowser,argv[argnum+1],&maxlines);
  545.         myscript.init(maxlines,ScriptBrowser);
  546.     argnum+=2;
  547.     scriptin=1;
  548.       }
  549.       else
  550.     showusage();
  551.     }
  552.     else
  553.      showusage();
  554.   }
  555.  if (!hidden)
  556.  {
  557.   if ((wposx<0)||(wposy<0))
  558.   {
  559.     minsize(290,330);
  560.     fl_show_form(Animate, FL_PLACE_FREE, TRUE, "Animate");
  561.   }
  562.   else
  563.   {
  564.     fl_set_form_position(Animate,wposx,wposy);
  565.     fl_show_form(Animate, FL_PLACE_POSITION, TRUE, "Animate");
  566.   }
  567.  }
  568.  
  569.  
  570.  if (filesin & !scriptin)
  571.  {
  572.   if (fils!=NULL)
  573.   {
  574.    if (fils[0]!=NULL)
  575.    {
  576.     fprintf(stdout,"(read geometry { define anim { LIST } } )\n");
  577.     fprintf(stdout,"(geometry animate : anim)\n");
  578.     fflush(stdout);
  579.     myscript.clean();
  580.     read_glob(ScriptBrowser,fils,&maxlines);
  581.     myscript.init(maxlines,ScriptBrowser);
  582.    }
  583.   }
  584.  }
  585.  
  586.  if (playin)
  587.    myscript.start_playing();
  588.  
  589.  while (retobj!=ExitButton)
  590.  {
  591.   if (block)
  592.    retobj = fl_do_forms();
  593.   else
  594.    retobj = fl_check_forms();
  595.   if (retobj!=NULL)
  596.   {
  597.    if (retobj==InfoButton)
  598.    {
  599.     fl_show_form(InfoPanel, FL_PLACE_SIZE, TRUE, "Info");
  600.    }
  601.    else
  602.    if (retobj==CommandButton)
  603.    {
  604.     fl_show_form(Command, FL_PLACE_SIZE, TRUE, "Command");
  605.    }
  606.    else
  607.    if (retobj==CommandInput)
  608.    {
  609.     fl_hide_form(Command);
  610.     myscript.add_command(fl_get_input(CommandInput));
  611.    }
  612.    else
  613.    if (retobj==CloseButton)
  614.    {
  615.     fl_hide_form(InfoPanel);
  616.    }
  617.    else
  618.    if (retobj==ExitButton)
  619.    {
  620.     exit(0);
  621.    }
  622.    if (retobj == PlayButton)
  623.    {
  624.     myscript.start_playing();
  625.     block=0;
  626.    }
  627.    else
  628.    if (retobj == StopButton)
  629.    {
  630.     myscript.stop_playing();
  631.     block=1;
  632.    }
  633.    else
  634.    if (retobj == LoopButton)
  635.    {
  636.     myscript.toggle_looping();
  637.    }
  638.    else
  639.    if (retobj == StepBButton)
  640.    {
  641.     if (fl_get_button(BounceButton))
  642.     {
  643.      myscript.toggle_bounce();
  644.      fl_set_button(BounceButton,0);
  645.     }
  646.     myscript.stop_playing();
  647.     myscript.change_direction(-1);
  648.     myscript.do_step();
  649.     block=1;
  650.    }
  651.    else
  652.    if (retobj == StepFButton)
  653.    {
  654.     if (fl_get_button(BounceButton))
  655.     {
  656.      myscript.toggle_bounce();
  657.      fl_set_button(BounceButton,0);
  658.     }
  659.     myscript.stop_playing();
  660.     myscript.change_direction(1);
  661.     myscript.do_step();
  662.     block=1;
  663.    }
  664.    else
  665.    /*
  666.    if (retobj == SwitchButton)
  667.    {
  668.     myscript.change_direction(0);
  669.    }*/
  670.    if (retobj == OnceButton)
  671.    {
  672.     myscript.once();
  673.    }
  674.    else
  675.    if (retobj == SpeedSlide)
  676.    {
  677.     myscript.change_speed(1000-(int)fl_get_slider_value(retobj));
  678.    }
  679.    else
  680.    if (retobj == BounceButton)
  681.    {
  682.     myscript.toggle_bounce();
  683.    }
  684.    else
  685.    if (retobj == ScriptBrowser)
  686.    {
  687.     line = fl_get_browser(ScriptBrowser);
  688.     if (line<0)
  689.      line = -line;
  690.     myscript.set_linenum(line);
  691.    }
  692.    else
  693.    if (retobj == ScriptButton)
  694.    {
  695.     fprintf(stdout,"(read geometry { define anim { LIST } } )\n");
  696.     fprintf(stdout,"(geometry animate : anim)\n");
  697.     fflush(stdout);
  698.     myscript.clean();
  699.     get_file(ScriptBrowser,fl_get_input(InputBox),&maxlines);
  700.     myscript.init(maxlines,ScriptBrowser);
  701.    }
  702.    else
  703.    if ((retobj == LoadButton)||(retobj == InputBox))
  704.    {
  705.     fnum=0;
  706.     fils=glob(fl_get_input(InputBox));
  707.     if (fils!=NULL)
  708.     {
  709.      if (fils[0]!=NULL)
  710.      {
  711.       fprintf(stdout,"(read geometry { define anim { LIST } } )\n");
  712.       fprintf(stdout,"(geometry animate : anim)\n");
  713.       fflush(stdout);
  714.       myscript.clean();
  715.       read_glob(ScriptBrowser,fils,&maxlines);
  716.       myscript.init(maxlines,ScriptBrowser);
  717.      }
  718.      free(fils);
  719.     }
  720.    }
  721.   }
  722.   myscript.animate();
  723.  }
  724. }
  725.